package com.zhenfan.pattern.register;

import java.io.Serializable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 使用map进行注册
 * 延迟加载，获取单例相当于懒加载集合
 *
 * @author wangmh
 * @create 2018-10-30 15:23
 **/
public class RegisterBean {

    private static Map<String, Object> map = new ConcurrentHashMap<>();

    private RegisterBean() {

    }

    public static Object getInstance(String beanName) {
        if (!map.containsKey(beanName)) {
            try {
                // map中没有对应的key，则使用反射创建
                map.put(beanName, Class.forName(beanName).newInstance());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return map.get(beanName);
    }
}
